package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsCollegeMapper;
import cn.kai.ums.model.UmsCollege;
import cn.kai.ums.service.UmsCollegeService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ums/v1/college")
@Api(tags = "学院控制类")
public class CollegeController {

    @Autowired
    private UmsCollegeService umsCollegeService;

    @Autowired
    private UmsCollegeMapper umsCollegeMapper;

    @ApiOperation("根据学院编号和名称进行模糊查询")
    @GetMapping
    public JsonData getCollegeList(@RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likecollege",required = false) String likecollege){
        Page<UmsCollege> collegePage = umsCollegeMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsCollege>lambdaQuery()
                        .like(UmsCollege::getCollegeNo,likecollege)
                        .or()
                        .like(UmsCollege::getCollegeName,likecollege));
        return JsonData.buildSuccess(collegePage);
    }

    @PostMapping
    public JsonData saveCollege(@RequestBody UmsCollege umsCollege){
        return JsonData.buildSuccess(umsCollegeService.save(umsCollege));
    }

    @PutMapping
    public JsonData updateCollege(@RequestBody UmsCollege umsCollege){
        return JsonData.buildSuccess(umsCollegeService.updateById(umsCollege));
    }


    @DeleteMapping
    public JsonData delCollege(@RequestParam Integer coid){
        return JsonData.buildSuccess(umsCollegeService.removeById(coid));
    }

    @PostMapping("/delBatchCollege")
    public JsonData delBatchCollege(@RequestBody List<Integer> coids){
        return JsonData.buildSuccess(umsCollegeService.removeByIds(coids));
    }
}
